package net.siufung.market.base.tenant.controller.regulation;

import com.google.common.base.Strings;
import lombok.AllArgsConstructor;
import net.siufung.core.exception.BizServiceException;
import net.siufung.core.model.VerifyRepeatDto;
import net.siufung.core.model.enums.MethodEventEnum;
import net.siufung.core.model.regulation.AbstractRegulation;
import net.siufung.market.base.tenant.controller.model.dto.TenantUpdateDto;
import net.siufung.market.base.tenant.mapper.ITenantMapper;
import net.siufung.market.base.tenant.mapper.ITenantProfileMapper;

/**
 * @author 陈建峰
 * @since 2022/5/30 10:28 下午
 */
@AllArgsConstructor
public class TenantUpdateRegulation extends AbstractRegulation<TenantUpdateDto> {

    private static final long serialVersionUID = 4529387407422691142L;

    private final ITenantMapper tenantMapper;
    private final ITenantProfileMapper tenantProfileMapper;

    @Override
    public boolean isSatisfiedBy(TenantUpdateDto tenantUpdateDto) {
        // 判断租户状态是否被激活
        VerifyRepeatDto<Integer> verifyRepeatDto = new VerifyRepeatDto<>();
        verifyRepeatDto.setMethodEventEnum(MethodEventEnum.UPDATE);
        verifyRepeatDto.setPk(tenantUpdateDto.getId());
        verifyRepeatDto.setVerifyKeyValue(tenantUpdateDto.getName());
        if(!tenantMapper.checkTenantNameNotRepeat(verifyRepeatDto)){
            throw new BizServiceException("租户名称不能重复！");
        }
        verifyRepeatDto.setVerifyKeyValue(tenantUpdateDto.getCode());
        if(!tenantMapper.checkTenantNameNotRepeat(verifyRepeatDto)){
            throw new BizServiceException("租户编码不能重复！");
        }
        if(!Strings.isNullOrEmpty(tenantUpdateDto.getContacts())){
            verifyRepeatDto.setVerifyKeyValue(tenantUpdateDto.getPhone());
            if(!tenantProfileMapper.checkTenantPhoneNotRepeat(verifyRepeatDto)){
                throw new BizServiceException("租户联系方式不能重复！");
            }
        }
        return Boolean.TRUE;
    }
}
